package com.pdf.utils;

import com.domain.WriteData;
import com.enums.Alignment;
import com.enums.LineEnum;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
import com.itextpdf.text.pdf.draw.LineSeparator;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;

import java.io.*;
import java.util.Map;
import java.util.Objects;

public class PDFUtils {

    public static PdfPTable createPdfTable(int numColumns, float[] cellSizeArray) {
        try {
            PdfPTable table = new PdfPTable(numColumns);
            // 设置总宽度
            table.setTotalWidth(490);
            table.setLockedWidth(true);
            // 设置每一列宽度
            if (ArrayUtils.isNotEmpty(cellSizeArray)) {
                table.setTotalWidth(cellSizeArray);
                table.setLockedWidth(true);
            }

            return table;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void addNoBorderCell(PdfPTable table, String text, Font paragraphFont, Alignment alignment) {
        PdfPCell cell = new PdfPCell(new Paragraph(text, paragraphFont));
        cell.setBorderWidth(1);
        cell.setMinimumHeight(30F);
        if (Alignment.CENTER == alignment) {
            // 设置水平居中
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            // 设置垂直居中（可选，取决于你的需求）
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        }
        table.addCell(cell);
    }

    public static void addNoBorderCell(PdfPTable table, WriteData writeData, Font paragraphFont, Alignment alignment) {
        for (int i = 0; i < 2; i++) {
            PdfPCell cell = new PdfPCell(new Paragraph(i == 0 ? writeData.getKey() : writeData.getStringValue(), paragraphFont));
            cell.setBorderWidth(0);
            cell.setMinimumHeight(30F);
            if (Alignment.CENTER == alignment) {
                // 设置水平居中
                cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                // 设置垂直居中（可选，取决于你的需求）
                cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            }
            table.addCell(cell);
        }
    }

    /**
     * 添加分割线
     *
     * @param document
     * @throws DocumentException
     */
    public static void addLine(Document document, LineEnum lineEnum, Map<String, PdfPTable> pdf_table_map, String group) {

        try {
            LineSeparator ls = new LineSeparator();
            ls.setLineWidth(1);
            ls.setLineColor(new BaseColor(179, 180, 164));
            Chunk chunk = new Chunk(ls);
            if (LineEnum.UP == lineEnum) {
                document.add(chunk);
                document.add(pdf_table_map.get(group));
                return;
            } else if (LineEnum.DOWN == lineEnum) {
                document.add(pdf_table_map.get(group));
                document.add(chunk);
                return;
            }
            if (Objects.nonNull(pdf_table_map.get(group))) {
                document.add(pdf_table_map.get(group));
            }

        }catch (DocumentException e) {
            e.printStackTrace();
        }

    }

    /**
     * 添加分割线
     *
     * @param document
     * @throws DocumentException
     */
    public static void addLine(Document document, LineEnum lineEnum, Paragraph paragraph) {

       try {
           LineSeparator ls = new LineSeparator();
           ls.setLineWidth(1);
           ls.setLineColor(new BaseColor(255, 0, 0));
           Chunk chunk = new Chunk(ls);
           if (LineEnum.UP == lineEnum) {
               document.add(chunk);
               document.add(paragraph);
               return;
           } else if (LineEnum.DOWN == lineEnum) {
               document.add(paragraph);
               document.add(chunk);
               return;
           }
           document.add(paragraph);
       }catch (DocumentException e) {
           e.printStackTrace();
       }
    }

    public static PdfPTable createTitleTable(int numColumns, Font font, String... titles) {
        PdfPTable table = new PdfPTable(numColumns);
        // 设置总宽度
        table.setTotalWidth(490);
        table.setLockedWidth(true);
        for (String title : titles) {
            addTitleCell(table, title, 1, font);
        }
        return table;
    }

    private static void addTitleCell(PdfPTable table, String text, float borderWidth, Font font) {
        Paragraph paragraph = new Paragraph(text, font);
        paragraph.setAlignment(1);
        PdfPCell cell = new PdfPCell(paragraph);
        cell.setBorderWidth(borderWidth);
        cell.setBackgroundColor(new BaseColor(245, 247, 251));
        //水平居中和垂直居中
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setMinimumHeight(30L);
        table.addCell(cell);
    }

    public static void addCell(PdfPTable table, String text, Font font) {
        if (StringUtils.isEmpty(text)) {
            text = " ";
        }
        Paragraph paragraph = new Paragraph(text, font);
        paragraph.setAlignment(1);
        PdfPCell cell = new PdfPCell(paragraph);
        //水平居中和垂直居中
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setMinimumHeight(30L);
        cell.setUseAscender(true);
        table.addCell(cell);
    }

    /**
     * 给pdf添加图片水印
     * @param waterMar
     * @param imgpath 图片路径
     * @throws Exception
     */
    public static void addImgWaterMark(PdfContentByte waterMar, String imgpath, BaseFont baseFont) {
        try {
            waterMar.beginText();

            PdfGState gs = new PdfGState();
            // 设置填充字体不透明度为0.4f
            gs.setFillOpacity(0.2f);
            waterMar.setFontAndSize(baseFont, 40);
            // 设置透明度
            waterMar.setGState(gs);
            // 设置水印对齐方式 水印内容 X坐标 Y坐标 旋转角度
            Image img = Image.getInstance(imgpath);
            img.setAbsolutePosition(200, 380);
            img.scaleAbsolute(200, 200);

            waterMar.addImage(img);
            // 设置水印颜色
            waterMar.setColorFill(BaseColor.GRAY);

            //结束设置
            waterMar.endText();
            waterMar.stroke();
        } catch (BadElementException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }catch (DocumentException e){
            e.printStackTrace();
        }
    }



}
